package com.taru.university.controller;

import com.taru.university.utils.Constant;
import com.taru.university.utils.JsonResult;
import com.taru.university.model.Datas;
import com.taru.university.service.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@CrossOrigin
public class DataController {
    private static Logger logger = LoggerFactory.getLogger(DataController.class);
    @Autowired
    DataService dataService;

    /**
     * 查询批次数据
     * @param dateBatchNumber
     * @return
     */
    @RequestMapping("/query/Data/details")
    @ResponseBody
    public Object queryDataByBatchNumber(String dateBatchNumber){
        JsonResult result = null;
        try {
         List<Datas> dataList = dataService.queryDataByBatchNumber(dateBatchNumber);
            logger.info("数据批次号{}",dateBatchNumber);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, dataList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    /**
     * 插入批次数据
     * @param data
     * @return
     */
    @RequestMapping("/insert/data")
    @ResponseBody
    public Object insertData(Datas data){
        JsonResult result = null;
        try {
          double num =  dataService.insertData(data);
            logger.info("数据{}",data);
            result = new JsonResult("添加成功", Constant.STATUC_SUCCESS, num);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常或者温度和湿度不是数字", Constant.STATUC_FAILURE);
        }
        return result;
    }

    /**
     * 清除数据
     * @param dateBatchNumber
     * @return
     */
    @RequestMapping("/delete/data")
    @ResponseBody
    public Object deleteData(String dateBatchNumber){
        JsonResult result = null;
        try {
            dataService.deleteData(dateBatchNumber);
            logger.info("数据批次号{}",dateBatchNumber);
            result = new JsonResult("删除成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("删除异常", Constant.STATUC_FAILURE);
        }
        return result;
    }
}
